import { z } from "zod";
import path from "path";
import { fileURLToPath } from "url";
import { getGradualTrySolutionPrompt } from "../../prompts/index.js";
import { getMemoryDir } from "../../utils/paths.js";

// 逐步尝试解决方案工具
// Gradual Try Solution Tool
export const gradualTrySolutionSchema = z.object({
  solutionCandidates: z
    .string()
    .min(30, {
      message: "解决方案候选列表不能少于30个字符，请提供更详细的方案描述",
    })
    .describe("来自全面思考阶段的解决方案候选列表和评估结果"),
  implementationStrategy: z
    .enum(['poc_first', 'prototype_driven', 'incremental', 'parallel'])
    .optional()
    .default('incremental')
    .describe("实施策略选择"),
  riskTolerance: z
    .enum(['conservative', 'moderate', 'aggressive'])
    .optional()
    .default('moderate')
    .describe("风险承受度级别"),
  validationCriteria: z
    .array(z.enum(['functionality', 'performance', 'reliability', 'usability', 'maintainability', 'scalability']))
    .optional()
    .default(['functionality', 'performance', 'reliability'])
    .describe("验证标准选择列表"),
  iterationLimit: z
    .number()
    .min(1)
    .max(10)
    .optional()
    .default(3)
    .describe("最大迭代次数限制")
});

export async function gradualTrySolution({
  solutionCandidates,
  implementationStrategy = 'incremental',
  riskTolerance = 'moderate',
  validationCriteria = ['functionality', 'performance', 'reliability'],
  iterationLimit = 3
}: z.infer<typeof gradualTrySolutionSchema>) {
  // 获取基础目录路径
  // Get base directory path
  const __filename = fileURLToPath(import.meta.url);
  const __dirname = path.dirname(__filename);
  const PROJECT_ROOT = path.resolve(__dirname, "../../..");
  const MEMORY_DIR = await getMemoryDir();

  // 使用prompt生成器获取最终prompt
  // Use prompt generator to get the final prompt
  const prompt = await getGradualTrySolutionPrompt({
    solutionCandidates,
    implementationStrategy,
    riskTolerance,
    validationCriteria,
    iterationLimit,
    memoryDir: MEMORY_DIR,
  });

  return {
    content: [
      {
        type: "text" as const,
        text: prompt,
      },
    ],
  };
}